package com.kk.util.test.thread.yield;

/**
 * @function:
 * @author: kevin
 * @date: 2018-09-07 10:59
 */
public class ThreadYield extends Thread {
    private String name;

    public ThreadYield(String name) {
        super(name);
    }

    public void run() {
        for (int i = 1; i <= 50; i++) {
            System.out.println("" + this.getName() + "-----" + i);
            // 当i为30时，该线程就会把CPU时间让掉，让其他或者自己的线程执行（也就是谁先抢到谁执行）
            if (i ==30) {
                this.yield();
            }
        }
    }

    public static void main(String[] args){
        ThreadYield yt1 = new ThreadYield("A");
        ThreadYield yt2 = new ThreadYield("B");
        yt1.start();
        yt2.start();
    }
}
